$(function () {
  //传参
  const seach = location.search;
  //console.log(seach);
  //转换页面传入参数数据 获取id
  const seachObj = new URLSearchParams(seach);
  // console.log(seachObj);
  const id = seachObj.get('id');
  //console.log(id);



  //发送请求，获取类别数据
  $.ajax({
    method: 'get',
    url: BigNew.category_list,
    data: {},
    success: (res) => {
      console.log(res);
      const htmlStr = template('list', res);
      $('[name="categoryId"]').html(htmlStr);
    }
  })

  //点击头像 切换图片
  $('.article_cover').click(function () {
    $("#inputCover").click();
  });
  $("#inputCover").change(function () {
    //获取值
    const file = $("#inputCover").get(0).files[0];
    //console.log(file);
    //把对象转换成浏览器URL路径
    const url = URL.createObjectURL(file);
    //const src = new URLSearchParams(file);
    //console.log(src);
    //console.log(url);
    $('.article_cover').prop('src', url);
  });

  //根据id获取文章信息

  $.ajax({
    method: 'get',
    url: BigNew.article_search,
    data: { id },
    success: (res) => {
      console.log(res);
      //渲染页面
      const { author, categoryId, content, cover, date, id, state, title, } = res.data;
      //console.log({ author, categoryId, content, cover, date, id, state, title, });
      $('#inputTitle').val(title);
      $('.article_cover').prop('src', cover);
      //让id为被选中项 value=id
      $('[name="categoryId"]').val(categoryId);
      $('[name="date"]').val(date);
      $('[name="content"]').val(content);


      //插件较大 等待需要时间
      /* setTimeout(() => {
        //渲染内容
        tinymce.activeEditor.setContent(content);
      }, 1000); */

    }
  })

  //加载日期插件
  jeDate('#data', {
    trigger: 'click',
    theme: { bgcolor: "#00A680", pnColor: "#00DDAA" },//绿色主题
    format: "YYYY-MM-DD",
    isinitVal: true,
  });




  tinymce.init({
    selector: '#mytextarea',
    height: '350px',
    language: 'zh_CN',
    directionality: 'ltl',
    browser_spellcheck: true,
    contextmenu: false,
    plugins: [
      "advlist autolink lists link image charmap print preview anchor",
      "searchreplace visualblocks code fullscreen",
      "insertdatetime media table contextmenu paste imagetools wordcount",
      "code"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code",
  });


  /* document.querySelector('#btn').onclick = function () {
    //3.获取tinymce编辑器文本
    console.log(tinymce.activeEditor.getContent());
  }; */

  //点击修改 发送请求
  $('[type="submit"]').click(function (e) {
    //阻止默认行为
    e.preventDefault();
    const state = $(this).attr('data-state');
    //console.log(state);
    const fd = new FormData(document.querySelector('#form'));
    fd.append('id', id);
    fd.append('state', state);
    fd.forEach((value, key) => {
      console.log(value, key);
    });
    //获取最新的修改数据显示到页面
    const content = tinymce.activeEditor.getContent();
    //获取最新的数据
    fd.set('content', content);
    //发送请求
    $.ajax({
      url: BigNew.article_edit,
      method: 'POST',
      data: fd,
      contentType: false,
      processData: false,
      success: (res) => {
        //console.log(res)
        alert(state + '成功');
        location.href = 'article_list.html';
      }
    })
  });

})